package com.appsindy.common.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appsindy.common.json.JSONResultCommon;
import com.appsindy.reserv.bo.Combo;
import com.appsindy.reserv.service.ReservationServices;
import com.appsindy.reserv.vo.CustomerVO;
import com.appsindy.reserv.vo.ItemVO;
import com.appsindy.reserv.vo.ReserveDetailVO;
import com.appsindy.reserv.vo.ReserveInfoVO;
import com.opensymphony.xwork2.Action;


public class ReserveDetailAction extends AppsindyBaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private int serviceHeaderId;
	private int serviceDetailId;
	private int groupId;
	private int customerId;
	private int typeOfRoomId;
	private int roomId;
	private int itemId;
	private Date dateGen;
	private Date dateFrom;
	private Date dateTo;
	
	private String[] colItemId;
	private String[] colItemQty;
	private String[] colUnitPrice;
	private String[] colTotalAmount;
	
	private String flagSave;
	
	private ReservationServices reservationServices;
	
	private ReserveDetailVO reserveDetailVO;
	
	private CustomerVO customerVO;
	
	private List<Combo> typeOfRoomList;
	private List<Combo> roomList;
	private List<CustomerVO> customerList;
	private List<Combo> itemList;
	private List<Combo> titleList;
	private List<Combo> genderList;
	private List<Combo> provinceList;
	private List<Combo> nationalityList;
	
	public String execute() {
		log.info("Init Reservation Detail ...");

		return initialReserve();
	}
	
	private String initialReserve(){
		log.info("Initial Reservation Information ...");
		setTypeOfRoomList(getReservationServices().getTypeOfRoomCombos());		
		setCustomerList(getReservationServices().getCustomerVOCombos());
		setItemList(getReservationServices().getItemCombos());
		
		//initial temp data
		int tmpCustomerId = getReserveDetailVO()!=null&&!"".equals(getReserveDetailVO().getCustomerId())?Integer.parseInt(getReserveDetailVO().getCustomerId()):0;
		
		if(reserveDetailVO != null && reserveDetailVO.getErrMsg() != null && !"".equals(reserveDetailVO.getErrMsg())){
			//???
		}else{
			if(getServiceDetailId() > 0){
				reserveDetailVO = getReservationServices().getReserveDetailById(getGroupId(), getServiceDetailId());
			}else if(getRoomId() > 0 && getDateGen() != null){
				reserveDetailVO = getReservationServices().getReserveDetailByRoomId(getGroupId(), getTypeOfRoomId(), getRoomId(), getDateGen());
			}else{
				reserveDetailVO = getReservationServices().getReserveDetail(getReserveDetailVO(), getGroupId());
			}
		}
		
		int typeId = 0;
		if(reserveDetailVO != null && (reserveDetailVO.getTypeOfRoomId()!=null&&Integer.parseInt(reserveDetailVO.getTypeOfRoomId()) > 0)){
			typeId = Integer.parseInt(reserveDetailVO.getTypeOfRoomId());
		}else{
			if(reserveDetailVO == null) reserveDetailVO = new ReserveDetailVO();
			reserveDetailVO.setTypeOfRoomId(""+typeOfRoomId);
			typeId = typeOfRoomId;			
		}
		if(serviceHeaderId > 0){
			reserveDetailVO.setServiceHeaderId(""+serviceHeaderId);
		}
		if(typeId > 0){
			setRoomList(getReservationServices().getRoomCombosByTypeOfRoom(typeId, dateFrom, dateTo));			
		}else{
			setRoomList(new ArrayList<Combo>());
		}
		
		if(tmpCustomerId > 0){
			CustomerVO customer = getReservationServices().getCustomerById(tmpCustomerId);
			if(customer != null){
				reserveDetailVO.setCustomerId(""+tmpCustomerId);
				reserveDetailVO.setCustomerName(customer.getCustomerName());
				reserveDetailVO.setCustomerCode(customer.getCustomerCode());
				reserveDetailVO.setPhoneNum(customer.getTelephone());
				reserveDetailVO.setMobileNum(customer.getMobile());
				reserveDetailVO.setEmail(customer.getEmail());
			}
		}
		
		return SUCCESS;
	}

	private void manageDetailList() throws Exception{
		log.info("manageDetailList ...");
        List<ItemVO> resultList = new ArrayList<ItemVO>();
        if(colItemQty != null && colItemQty.length > 0){
            int size			= colItemQty.length;

            ItemVO vo	= null;
            for(int i=0; i<size; i++){
                vo              = new ItemVO();
                vo.setItemId(colItemId[i]);
                vo.setItemQty(colItemQty[i]);
            	vo.setUnitPrice(colUnitPrice[i]);
            	vo.setTotalAmount(colTotalAmount[i]);
                resultList.add(vo);
            }
        }
        getReserveDetailVO().setItemList(resultList);
	}
	
	public String save(){
		log.info("Save Reserve Detail...");
		
		try{
			manageDetailList();
		}catch(Exception e){			
		}
		
		ReserveDetailVO temp = getReserveDetailVO();
		reserveDetailVO = getReservationServices().saveReserveDetail(getReserveDetailVO());
		if(reserveDetailVO != null && !"".equals(reserveDetailVO.getServiceDetailId())){
			groupId 		= reserveDetailVO.getGroupInfoVO().getGroupId();
			serviceDetailId = Integer.parseInt(reserveDetailVO.getServiceDetailId());
		}
		if(reserveDetailVO != null && reserveDetailVO.getErrMsg() != null && !"".equals(reserveDetailVO.getErrMsg())){
			addActionError(getText("reserve.typeofroom.error.qtyroomnotenough") + " " + reserveDetailVO.getErrMsg());
			reserveDetailVO = temp;
		}else{			
			flagSave = "Y";			
		}		
		return initialReserve();
	}
	
    @SuppressWarnings("finally")
	public String getCustomerJSON() {
        Map<String, String> errorMap = new HashMap<String, String>();
        errorMap.put("errorStr", "Not found customer.");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try {
            if(customerId > 0){
                CustomerVO customerVO = getReservationServices().getCustomerById(customerId);

                jsonResultCommon = new JSONResultCommon(Action.SUCCESS, customerVO);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }
    
	@SuppressWarnings("finally")
	public String getRoomListJSON(){
    	log.info("getRoomListJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't get room list");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	List<Combo> result = getReservationServices().getRoomCombosByTypeOfRoom(typeOfRoomId, dateFrom, dateTo);
            jsonResultCommon = new JSONResultCommon(Action.SUCCESS, result);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }
	
    @SuppressWarnings("finally")
	public String getDetailRoomRateJSON(){
    	log.info("getRoomRateJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't get room rate");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	ReserveInfoVO infoVO = getReservationServices().getDetailRoomRate(groupId, dateFrom, dateTo, typeOfRoomId, roomId);
        	
        	if(infoVO.getErrCode() < 0){
        		infoVO.setErrMsg(getText("reserve.typeofroom.error.manyroomrate"));
        	}
            jsonResultCommon = new JSONResultCommon(Action.SUCCESS, infoVO);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }    
    
	public String newCustomer() {
		log.info("newCustomer Popup ...");
		
		setTitleList(getReservationServices().getTitleCombos());
		setGenderList(getReservationServices().getGenderCombos());
		setProvinceList(getReservationServices().getProvinceCombos());
		setNationalityList(getReservationServices().getNationalityCombos());
		
		//default
		if(customerVO == null) customerVO = new CustomerVO();
		customerVO.setTitle("42");	//คุณ
		
		if(customerId > 0){
			customerVO = getReservationServices().getCustomerById(customerId);
		}

		return "customerPopup";
	}
	
	public String addCustomer() {
		log.info("addCustomer ...");
		
		customerVO = getReservationServices().addCustomer(customerVO);

		flagSave = "Y";
		
		return newCustomer();
	}
	
	public String editCustomer() {
		log.info("editCustomer ...");
		
		customerVO = getReservationServices().editCustomer(customerVO);

		flagSave = "Y";

		return newCustomer();
	}	

    @SuppressWarnings("finally")
	public String getItemPriceJSON() {
        Map<String, String> errorMap = new HashMap<String, String>();
        errorMap.put("errorStr", "Not found item price.");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try {
            if(itemId > 0){
                ItemVO itemVO = getReservationServices().getItemById(itemId);

                jsonResultCommon = new JSONResultCommon(Action.SUCCESS, itemVO);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }
	
	public List<Combo> getItemList() {
		return itemList;
	}

	public void setItemList(List<Combo> itemList) {
		this.itemList = itemList;
	}

	public ReservationServices getReservationServices() {
		return reservationServices;
	}

	public void setReservationServices(ReservationServices reservationServices) {
		this.reservationServices = reservationServices;
	}

	public int getServiceDetailId() {
		return serviceDetailId;
	}

	public void setServiceDetailId(int serviceDetailId) {
		this.serviceDetailId = serviceDetailId;
	}

	public int getRoomId() {
		return roomId;
	}

	public void setRoomId(int roomId) {
		this.roomId = roomId;
	}

	public ReserveDetailVO getReserveDetailVO() {
		return reserveDetailVO;
	}

	public void setReserveDetailVO(ReserveDetailVO reserveDetailVO) {
		this.reserveDetailVO = reserveDetailVO;
	}

	public List<CustomerVO> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<CustomerVO> customerList) {
		this.customerList = customerList;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}

	public List<Combo> getTypeOfRoomList() {
		return typeOfRoomList;
	}

	public void setTypeOfRoomList(List<Combo> typeOfRoomList) {
		this.typeOfRoomList = typeOfRoomList;
	}

	public List<Combo> getRoomList() {
		return roomList;
	}

	public void setRoomList(List<Combo> roomList) {
		this.roomList = roomList;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public Date getDateGen() {
		return dateGen;
	}

	public void setDateGen(Date dateGen) {
		this.dateGen = dateGen;
	}

	public Date getDateFrom() {
		return dateFrom;
	}

	public void setDateFrom(Date dateFrom) {
		this.dateFrom = dateFrom;
	}

	public Date getDateTo() {
		return dateTo;
	}

	public void setDateTo(Date dateTo) {
		this.dateTo = dateTo;
	}

	public int getTypeOfRoomId() {
		return typeOfRoomId;
	}

	public void setTypeOfRoomId(int typeOfRoomId) {
		this.typeOfRoomId = typeOfRoomId;
	}

	public String[] getColItemId() {
		return colItemId;
	}

	public void setColItemId(String[] colItemId) {
		this.colItemId = colItemId;
	}

	public String[] getColItemQty() {
		return colItemQty;
	}

	public void setColItemQty(String[] colItemQty) {
		this.colItemQty = colItemQty;
	}

	public String[] getColUnitPrice() {
		return colUnitPrice;
	}

	public void setColUnitPrice(String[] colUnitPrice) {
		this.colUnitPrice = colUnitPrice;
	}

	public String[] getColTotalAmount() {
		return colTotalAmount;
	}

	public void setColTotalAmount(String[] colTotalAmount) {
		this.colTotalAmount = colTotalAmount;
	}

	public CustomerVO getCustomerVO() {
		return customerVO;
	}

	public void setCustomerVO(CustomerVO customerVO) {
		this.customerVO = customerVO;
	}

	public List<Combo> getTitleList() {
		return titleList;
	}

	public void setTitleList(List<Combo> titleList) {
		this.titleList = titleList;
	}

	public List<Combo> getGenderList() {
		return genderList;
	}

	public void setGenderList(List<Combo> genderList) {
		this.genderList = genderList;
	}

	public List<Combo> getProvinceList() {
		return provinceList;
	}

	public void setProvinceList(List<Combo> provinceList) {
		this.provinceList = provinceList;
	}

	public List<Combo> getNationalityList() {
		return nationalityList;
	}

	public void setNationalityList(List<Combo> nationalityList) {
		this.nationalityList = nationalityList;
	}

	public int getServiceHeaderId() {
		return serviceHeaderId;
	}

	public void setServiceHeaderId(int serviceHeaderId) {
		this.serviceHeaderId = serviceHeaderId;
	}

	public String getFlagSave() {
		return flagSave;
	}

	public void setFlagSave(String flagSave) {
		this.flagSave = flagSave;
	}

	public int getItemId() {
		return itemId;
	}

	public void setItemId(int itemId) {
		this.itemId = itemId;
	}
	
	
    
}
